package com.yunbiao.ybbusstopplate.utils.core.play;

import android.graphics.Point;
import android.text.TextUtils;
import android.view.Display;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.yunbiao.ybbusstopplate.utils.core.TYTool;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

public class LayoutJsonTool {

    /**
     * 背景音乐消息解析
     *
     * @param layoutMenuJson
     * @return
     */
    static String getLayoutBackMuisc(String layoutMenuJson, int position) {
        if (layoutMenuJson.equals("null") || layoutMenuJson.equals("") || layoutMenuJson.equals("faile")) {
            return "";
        }
        String bgMusic = "";
        String layoutListPlayTime = "";
        JSONTokener jsonParser = new JSONTokener(layoutMenuJson);
        try {
            JSONObject urlJson = (JSONObject) jsonParser.nextValue();
            JSONArray jsonArray;
            try {
                JSONObject layoutJson = new JSONObject(layoutMenuJson);
                if (layoutJson.has("playTime"))
                    layoutListPlayTime = layoutJson.getString("playTime");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (TextUtils.isEmpty(layoutListPlayTime)) {//单个布局 背景音乐
                jsonArray = (JSONArray) TYTool.getJsonObj(urlJson, "move", null);
            } else {//混合布局 背景音乐
                JSONArray layoutList = urlJson.getJSONArray("layoutList");
                JSONObject jsonObject1 = layoutList.getJSONObject(position);
                jsonArray = (JSONArray) TYTool.getJsonObj(jsonObject1, "move", null);
            }

            if (jsonArray != null && jsonArray.length() > 0) {
                int length = jsonArray.length();
                for (int i = 0; i < length; i++) {
                    JSONObject moveObj = (JSONObject) jsonArray.get(i);
                    JSONObject musicJsonObj = (JSONObject) TYTool.getJsonObj(moveObj, "bgmusic", null);
                    if (musicJsonObj != null) {
                        String music = (String) TYTool.getJsonObj(musicJsonObj, "music", null);
                        if (music != null) {
                            bgMusic = music;
                        }
                        break;
                    }
                }
            }

            if (!bgMusic.equals("null") && !bgMusic.equals("") && (bgMusic.startsWith("http:")||bgMusic.startsWith("https:"))) {
                bgMusic = bgMusic.substring(bgMusic.lastIndexOf("/") + 1, bgMusic.length());
            } else if (bgMusic.equals("播放本地背景音乐")) {
                bgMusic = "播放本地背景音乐";
            } else {
                bgMusic = "";
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return bgMusic;
    }

    /**
     * 获取layoutMenu
     *
     * @param layoutMenuJson
     * @return
     */
    public static LayoutMenu getLayoutMenu(String layoutMenuJson) {
        LayoutMenu layoutMenu = new LayoutMenu();
        if (layoutMenuJson.equals("") || layoutMenuJson.equals("faile")) {
            layoutMenu.setShow(false);
        } else {
            JSONTokener jsonParser = new JSONTokener(layoutMenuJson);
            try {
                JSONObject urlJson = (JSONObject) jsonParser.nextValue();
                JSONObject layoutJson = urlJson.getJSONObject("header");
                boolean enabled = layoutJson.getBoolean("enabled");
                if (enabled) {
                    String logoimg = layoutJson.getString("logoimg");
                    String weatherShow = layoutJson.getString("weatherShow");
                    String address = layoutJson.getString("address");
                    String timeShow = layoutJson.getString("timeShow");
                    String timeFormat = layoutJson.getString("timeFormat");
                    String fontFamily = layoutJson.getString("fontFamily");
                    String fontSize = layoutJson.getString("fontSize");
                    String fontColor = layoutJson.getString("fontColor");
                    String backGround = layoutJson.getString("background");

                    boolean timeShowBoolean = false;
                    if (timeShow.equals("1")) {
                        timeShowBoolean = true;
                    }
                    boolean weatherShowBoolean = false;
                    if (weatherShow.equals("1")) {
                        weatherShowBoolean = true;
                    }

                    layoutMenu.setShow(true);
                    layoutMenu.setTimeShow(timeShowBoolean);
                    layoutMenu.setWeatherShow(weatherShowBoolean);
                    layoutMenu.setBackGround(backGround);
                    layoutMenu.setAddress(address);
                    layoutMenu.setFontColor(fontColor);
                    layoutMenu.setFontFamily(fontFamily);
                    fontSize = fontSize.replaceAll("px", "");
                    layoutMenu.setFontSize(Float.valueOf(fontSize));
                    layoutMenu.setTimeFormat(timeFormat.replace("hh", "kk").replace("HH", "kk"));

                    if (logoimg.startsWith("http:")||logoimg.startsWith("https:")) {
                        logoimg = logoimg.substring(logoimg.lastIndexOf("/") + 1, logoimg.length());
                    }
                    layoutMenu.setLogoImage(logoimg);
                } else {
                    layoutMenu.setShow(false);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return layoutMenu;
    }

    /**
     * 获取getLayoutFoot
     *
     * @param layoutMenuJson
     * @return
     */
    public static LayoutFoot getLayoutFoot(String layoutMenuJson) {
        LayoutFoot layoutFoot = new LayoutFoot();
        if (layoutMenuJson.equals("") || layoutMenuJson.equals("faile")) {
            layoutFoot.setEnabled(false);
        } else {
            JSONTokener jsonParser = new JSONTokener(layoutMenuJson);
            try {
                JSONObject urlJson = (JSONObject) jsonParser.nextValue();
                JSONObject layoutJson = urlJson.getJSONObject("footer");
                boolean enabled = layoutJson.getBoolean("enabled");
                if (enabled) {
                    String footerText = layoutJson.getString("footerText");
                    String playTime = layoutJson.getString("playTime");
                    String isPlay = layoutJson.getString("isPlay");
                    String fontFamily = layoutJson.getString("fontFamily");
                    String fontSize = layoutJson.getString("fontSize");
                    String fontColor = layoutJson.getString("fontColor");
                    String background = layoutJson.getString("background");

                    layoutFoot.setEnabled(true);
                    layoutFoot.setFooterText(footerText);
                    try {
                        float playTimef = Float.parseFloat(playTime);
                        layoutFoot.setPlayTime(playTimef);
                    } catch (Exception e) {
                        layoutFoot.setPlayTime(1f);
                    }

                    layoutFoot.setIsPlay(isPlay);
                    layoutFoot.setBackground(background);
                    layoutFoot.setFontColor(fontColor);
                    layoutFoot.setFontFamily(fontFamily);

                    fontSize = fontSize.replaceAll("px", "");
                    layoutFoot.setFontSize(Float.parseFloat(fontSize));
                } else {
                    layoutFoot.setEnabled(false);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return layoutFoot;
    }

    /**
     * 获取布局
     */
    public static List<LayoutInfo> getLayoutInfo(String json) {

        List list = new ArrayList();
        try {

            JSONTokener jsonParser = new JSONTokener(json);

            JSONObject objJson = (JSONObject) jsonParser.nextValue();

            JSONArray person = objJson.getJSONArray("center");

            for (int i = 0; i < person.length(); i++) {

                LayoutInfo layoutInfo = new LayoutInfo();

                JSONObject jsonObject = (JSONObject) person.get(i);
                String id = (String) TYTool.getJsonObj(jsonObject, "id", "");
                Integer type = Integer.parseInt((String) TYTool.getJsonObj(jsonObject, "type", null));

                layoutInfo.setId(id);
                layoutInfo.setType(type);

                Container containerObj = getContainer(jsonObject);
                layoutInfo.setContainer(containerObj);



                if (type == 2) {// 文本消息处理
                    TextDetail textDetail = getTextDetail(jsonObject);
                    layoutInfo.setTextDetail(textDetail);
                } else if (type == 1 || type == 6 || type == 8) {// 图片消息处理
                    ImageDetail imageDetail = getImageDetail(jsonObject);
                    layoutInfo.setImageDetail(imageDetail);
                } else if (type == 3) {// 视频消息处理
                    VideoDetail imageDetail = getVideoDetail(jsonObject);
                    layoutInfo.setVideoDetail(imageDetail);
                } else if (type == 4) {// 微信内容解析

                } else if (type == 5) {// 网页内容解析
                    WebDetail webDetail = getWebDetail(jsonObject);
                    layoutInfo.setWebDetail(webDetail);
                } else if (type == 13) {//排队叫号解析

                } else if (type == 14) {//windowType
                    try {
                        int windowType = Integer.parseInt((String) TYTool.getJsonObj(jsonObject, "windowType", "1"));
                        String cityCode = String.valueOf(TYTool.getJsonObj(jsonObject, "cityCode", ""));
                        layoutInfo.setWindowType(windowType);
                        layoutInfo.setCityCode(cityCode);
                    } catch (ClassCastException e) {
                        e.printStackTrace();
                    }
                } else if (type == 12) {//摄像头 或者 HDMI

                } else if (type == 17) {

                }else if (type==18){

                }

                // 显示内容处理
                // JSONArray content = jsonObject.getJSONArray("content");
                JSONArray content = (JSONArray) TYTool.getJsonObj(jsonObject, "content", null);
                if (content != null) {
                    String[] contentArray = new String[content.length()];
                    for (int j = 0; j < content.length(); j++) {
                        String con = (String) content.get(j);
                        if ((con.startsWith("http:")||con.startsWith("https:")) && type != 5) {
                            con = con.substring(con.lastIndexOf("/") + 1, con.length());
                        }
                        contentArray[j] = con;
                    }
                    layoutInfo.setContent(contentArray);
                }
                list.add(layoutInfo);
            }
            parseMovePanel(objJson, list);
        } catch (Exception e) {
            e.printStackTrace();
            list = null;
        }
        return list;
    }



    /**
     * 移动窗口处理
     */
    private static void parseMovePanel(JSONObject objJson, List list) throws JSONException {
        JSONArray move = (JSONArray) TYTool.getJsonObj(objJson, "move", null);
        if (move != null) {
            for (int i = 0; i < move.length(); i++) {
                LayoutInfo layoutInfo = new LayoutInfo();

                JSONObject jsonObject = (JSONObject) move.get(i);
                String type = (String) TYTool.getJsonObj(jsonObject, "type", null);

                Container containerObj = getContainer(jsonObject);
                layoutInfo.setContainer(containerObj);

                if (type.contains("_1")) {// 图片消息处理
                    layoutInfo.setType(1);
                    ImageDetail imageDetail = getImageDetail(jsonObject);
                    layoutInfo.setImageDetail(imageDetail);
                } else if (type.contains("_3")) {// 音乐
                    layoutInfo.setType(-3);
                } else if (type.contains("_2")) {//文本
                    layoutInfo.setType(2);
                    TextDetail textDetail = getTextDetail(jsonObject);
                    layoutInfo.setTextDetail(textDetail);
                }

                // 显示内容处理
                JSONArray content = (JSONArray) TYTool.getJsonObj(jsonObject, "content", null);
                if (content != null) {
                    String[] contentArray = new String[content.length()];
                    for (int j = 0; j < content.length(); j++) {
                        String con = (String) content.get(j);
                        if (con.startsWith("http:")||con.startsWith("https:")) {
                            con = con.substring(con.lastIndexOf("/") + 1, con.length());
                        }
                        contentArray[j] = con;
                    }
                    layoutInfo.setContent(contentArray);
                }
                list.add(layoutInfo);
            }
        }
    }

    /**
     * 获取布局的位置
     */
    private static int allHeight=0;
    public static LayoutPosition getViewPostion(LayoutInfo layoutInfo, WindowManager wm) {
        Display display = wm.getDefaultDisplay();
        Point point = new Point();
        display.getRealSize(point);
        int width = point.x;
        int height = point.y - CreateElement.TOOL_HEIGHT - CreateElement.FOOT_HEIGHT;

        LayoutPosition layoutPostion = new LayoutPosition();
        String widths = layoutInfo.getContainer().getWidth();
        String heights = layoutInfo.getContainer().getHeight();
        String lefts = layoutInfo.getContainer().getLeft();
        String tops = layoutInfo.getContainer().getTop();

        float layoutHeight = Float.valueOf(heights.substring(0, heights.indexOf("%"))) / 100;
        float layoutWidth = Float.valueOf(widths.substring(0, widths.indexOf("%"))) / 100;
        float layoutLeft = Float.valueOf(lefts.substring(0, lefts.indexOf("%"))) / 100;
        float layoutTop = Float.valueOf(tops.substring(0, tops.indexOf("%"))) / 100;

        Float layHeight = (layoutHeight * height);
        Float layWidth = (layoutWidth * width);
        Float layLeft = (layoutLeft * width);
        Float layTop = (layoutTop * height);

//        Log.d("layoutjsontool", "getViewPostion: top->"+layTop+",height->"+layHeight);
//        Log.d("layoutjsontool", "getViewPostion: top1->"+TYTool.getFloatToInt(layTop, "#")+",height1->"+TYTool.getFloatToInt(layHeight, "#"));

        // TODO: 2021-06-16 三分屏白线处理
        Integer fHeight = TYTool.getFloatToInt(layHeight, "#");
        int ftop = TYTool.getFloatToInt(layTop, "#") + CreateElement.TOOL_HEIGHT;
        String id = layoutInfo.getId();
        if (!TextUtils.isEmpty(id)){
            if (id.contains("col1")||id.contains("row1_1")||id.contains("row2_1")){
                allHeight=allHeight+fHeight;
            }
            int spHeight = height - allHeight;//剩余高度
//        Log.d("layoutjsontool", "getViewPostion: spHeight->"+spHeight);
            if (spHeight>0&&spHeight<=2){//展示不全有白线
                fHeight=fHeight+spHeight;
                ftop=ftop-spHeight;
                allHeight=0;
            }else if (spHeight<0&&spHeight>=-2){//重叠几像素
                int abs = Math.abs(spHeight);
                fHeight=fHeight-abs;
                ftop=ftop+abs;
                allHeight=0;
            }else if (spHeight==0){
                allHeight=0;
            }
        }
        layoutPostion.setWidth(TYTool.getFloatToInt(layWidth, "#"));
        layoutPostion.setLeft(TYTool.getFloatToInt(layLeft, "#"));
        layoutPostion.setHeight(fHeight);
        layoutPostion.setTop(ftop);



        return layoutPostion;
    }


    private static Container getContainer(JSONObject container) {
        JSONObject containerJson = (JSONObject) TYTool.getJsonObj(container, "container", null);
        if (containerJson != null) {
            String width = (String) TYTool.getJsonObj(containerJson, "width", "");
            String height = (String) TYTool.getJsonObj(containerJson, "height", "");
            String top = (String) TYTool.getJsonObj(containerJson, "top", "");
            String left = (String) TYTool.getJsonObj(containerJson, "left", "");

            Container containerObj = new Container();
            containerObj.setWidth(width);
            containerObj.setHeight(height);
            containerObj.setTop(top);
            containerObj.setLeft(left);

            return containerObj;
        } else {
            return null;
        }
    }


    private static TextDetail getTextDetail(JSONObject jsonObject) {
        JSONObject textDetail = (JSONObject) TYTool.getJsonObj(jsonObject, "textDetail", null);
        if (textDetail != null) {
            Boolean isPlay = Boolean.parseBoolean((String) TYTool.getJsonObj(textDetail, "isPlay", "false"));
            String playTime = (String) TYTool.getJsonObj(textDetail, "playTime", "5");
            String fontSize = (String) TYTool.getJsonObj(textDetail, "fontSize", "");
            String fontColor = (String) TYTool.getJsonObj(textDetail, "fontColor", "");
            String fontFamily = (String) TYTool.getJsonObj(textDetail, "fontFamily", "");
            String background = (String) TYTool.getJsonObj(textDetail, "background", "");
            String playType = (String) TYTool.getJsonObj(textDetail, "playType", "0"); //滚动方式
            Integer dataType = (Integer) TYTool.getJsonObj(textDetail, "dataType", 0); //汉王 空气监控
            String textAlign = (String) TYTool.getJsonObj(textDetail, "textAlign", ""); //对齐方式 靠左  居中  靠右

            TextDetail textDetailObj = new TextDetail();
            textDetailObj.setIsPlay(isPlay);
            textDetailObj.setPlayTime(Float.parseFloat(playTime));
            fontSize = fontSize.replaceAll("px", "");
            textDetailObj.setFontSize(Integer.parseInt(fontSize));
            textDetailObj.setFontColor(fontColor);
            textDetailObj.setFontFamily(fontFamily);
            textDetailObj.setBackground(background);
            textDetailObj.setPlayType(playType);
            textDetailObj.setDataType(dataType);
            textDetailObj.setTextAlign(textAlign);

            return textDetailObj;
        } else {
            return null;
        }
    }

    private static ImageDetail getImageDetail(JSONObject jsonObject) {
        JSONObject imageDetail = (JSONObject) TYTool.getJsonObj(jsonObject, "imageDetail", null);
        if (imageDetail != null) {
            String isPlay = (String) TYTool.getJsonObj(imageDetail, "isAutoPlay", "");
            String playTime = (String) TYTool.getJsonObj(imageDetail, "playTime", "");
            String imagePlayType = (String) TYTool.getJsonObj(imageDetail, "imagePlayType", "0");
            ImageDetail imageDetailObj = new ImageDetail();
            imageDetailObj.setPlayTime(playTime);
            imageDetailObj.setIsAutoPlay(isPlay);
            imageDetailObj.setImagePlayType(imagePlayType);
            return imageDetailObj;
        } else {
            return null;
        }
    }

    private static VideoDetail getVideoDetail(JSONObject videoJson) {
        JSONObject videoDetail = (JSONObject) TYTool.getJsonObj(videoJson, "videoDetail", null);
        if (videoDetail != null) {
            boolean loop = (Boolean) TYTool.getJsonObj(videoDetail, "loop", true);
            VideoDetail videoDetailObj = new VideoDetail();
            videoDetailObj.setLoop(loop);
            return videoDetailObj;
        } else {
            return null;
        }
    }



    private static WebDetail getWebDetail(JSONObject jsonObject) {
        JSONObject webmsg = (JSONObject) TYTool.getJsonObj(jsonObject, "webmsg", null);
        if (webmsg != null) {
            String autoFlus = (String) TYTool.getJsonObj(webmsg, "autoflus", "");
            String flusTime = (String) TYTool.getJsonObj(webmsg, "flustime", "");
            String webType = (String) TYTool.getJsonObj(webmsg, "webType", "");

            WebDetail webDetail = new WebDetail();
            if ("0".equals(autoFlus)) {
                webDetail.setAutoFlus(true);
            } else {
                webDetail.setAutoFlus(false);
            }
            webDetail.setFlusTime(flusTime);
            webDetail.setWebType(webType);

            return webDetail;
        } else {
            return null;
        }
    }




}
